package com.example.smallmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smallmall.common.api.CommonResult;
import com.example.smallmall.entity.SpAttribute;
import com.example.smallmall.entity.SpCategory;
import com.example.smallmall.service.ISpAttributeService;
import com.example.smallmall.service.ISpCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WSGL
 * @since 2020-08-05
 */
@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class SpCategoryController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SpCategoryController.class);

    @Resource
    private ISpCategoryService iSpCategoryService;
    @Resource
    private ISpAttributeService iSpAttributeService;

    @ApiOperation("商品分类")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Map<String, Object>>> listCategory() {
        List<Map<String, Object>> maps = iSpCategoryService.listMaps(new QueryWrapper<SpCategory>().eq("cat_level", 0));
        if (maps != null) {
            for (Map<String, Object> map : maps) {
                List<SpCategory> list = iSpCategoryService.list(new QueryWrapper<SpCategory>().eq("cat_pid", map.get("catId")).eq("cat_level", 1));
                map.put("children", list);
            }
            if (maps.isEmpty()) {
                return CommonResult.failed("操作失败");
            } else {
                return CommonResult.success(maps);
            }
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过二级分类ID找到子属的三级分类")
    @RequestMapping(value = "/findCategory/{catId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SpCategory>> listCategoryById(@PathVariable("catId") Integer id) {
        List<SpCategory> spCategories = iSpCategoryService.list(new QueryWrapper<SpCategory>().lambda().eq(SpCategory::getCatPid, id));
        if (!spCategories.isEmpty()) {
            return CommonResult.success(spCategories);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过分类查找规格属性种类")
    @RequestMapping(value = "/findAttributeByCategory/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Map<String, Object>>> findAttributeByCategory(@PathVariable("id") Integer id) {
        List<Map<String, Object>> maps = iSpAttributeService.listMaps(new QueryWrapper<SpAttribute>().eq("cat_id", id));
        if (maps != null && !maps.isEmpty()) {
            return CommonResult.success(maps);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过ID查找分类名称")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, Object>> listCategoryByID(@PathVariable("id") Integer id) {
        QueryWrapper<SpCategory> queryWrapper = new QueryWrapper<>();
        //queryWrapper.select("cat_id", "cat_name", "cat_pid").eq("cat_id", id);
        queryWrapper.lambda().select(SpCategory::getCatId, SpCategory::getCatName, SpCategory::getCatPid).eq(SpCategory::getCatId, id);
        Map<String, Object> objectMap = iSpCategoryService.getMap(queryWrapper);
        Object obj = 0;
        if (objectMap.get("catPid") == obj) {
            objectMap.remove("catPid");
        }
        if (!objectMap.isEmpty()) {
            return CommonResult.success(objectMap);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("添加分类信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<SpCategory> createCategory(@RequestBody SpCategory spCategory) {
        boolean save = iSpCategoryService.save(spCategory);
        if (save) {
            LOGGER.info("createCategory success: {}", spCategory);
            return CommonResult.success(spCategory);
        } else {
            LOGGER.info("createCategory failed: {}", spCategory);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过ID修改分类信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<SpCategory> updateCategory(@PathVariable("id") Integer id, @RequestBody SpCategory spCategory) {
        QueryWrapper<SpCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SpCategory::getCatId, id);
        boolean update = iSpCategoryService.update(spCategory, queryWrapper);
        if (update) {
            LOGGER.info("updateCategory success: {}", spCategory);
            return CommonResult.success(spCategory);
        } else {
            LOGGER.info("updateCategory failed: {}", spCategory);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过ID删除分类信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Boolean> deleteCategory(@PathVariable("id") Integer id) {
        boolean remove = iSpCategoryService.removeById(id);
        if (remove) {
            LOGGER.info("deleteCategory success: {}", id);
            return CommonResult.success(true);
        } else {
            LOGGER.info("deleteCategory failed: {}", id);
            return CommonResult.failed("操作失败");
        }
    }
}
